package com.spl.msplayer;


import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.ProgressBar;
import android.widget.VideoView;
import com.spl.msplayer.MainScreen;
public class VideoPlayer extends Activity implements SurfaceHolder.Callback {

	SurfaceView cameraView;
	SurfaceHolder holder;
	
	private MediaController mcontroller;
	private Handler handler = new Handler();
	
	private static VideoPlayer instance;
	private PlayerService service;
	
	int orientation;
	String videopath;
	
	boolean isFirstStart;
	boolean isFromNotification = false;
	
	ProgressBar waiting;
	
	boolean isBGPlay = true;
	
	public static VideoPlayer getInstance() {
		return instance;
	}
	
	private class InitTask extends AsyncTask<Integer, Integer, Void> {
		
		int interval;
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			this.interval = params[0];
			long currMili = System.currentTimeMillis();
			while((interval * 1000) > (System.currentTimeMillis() - currMili) ) {
				
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			super.onPostExecute(result);
			
			service.prepair(true);
		}

		@Override
		protected void onProgressUpdate(Integer... values) {

			super.onProgressUpdate(values);
			
		}

		
	}
	
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.videoplayer);
		getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LOW_PROFILE);
		service = PlayerService.getInstance();
		
		waiting = (ProgressBar)findViewById(R.id.waiting);
		
		mcontroller = new MediaController(this);
		Intent intent = getIntent();
		if(intent.getBooleanExtra(MainScreen.RESUME_FROM_NOTIFICATION, false)) {
			Log.i("SUMPRO", "From notification");
			orientation = service.getVideoOrientation();
			isFromNotification = true;
		} else {
			videopath = intent.getStringExtra(MainScreen.VIDEO_PATH_KEY);
			orientation = intent.getIntExtra(MainScreen.VIDEO_ORIENTATION_KEY, 0);
			service.setVideoOrientation(orientation);
			service.setOnlineStreaming(intent.getBooleanExtra(MainScreen.ONLINE_STREAMING, false));
			service.reset();
		}
		cameraView = (SurfaceView)findViewById(R.id.playerView);
		holder = cameraView.getHolder();
		holder.addCallback(this);
		

		setRequestedOrientation(orientation);

		isFirstStart = true;
		
		instance = this;
	}

	
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		Log.i("SUMPRO", "surfaceCreated");
		service.getPlayer().setDisplay(holder);
		mcontroller.setEnabled(true);
		mcontroller.setMediaPlayer(service);
		mcontroller.setAnchorView(findViewById(R.id.playerView));
		if(!isFromNotification && isFirstStart) {
			//waiting.se
			
			service.setVideopath(videopath);
			//service.setVideopath("http://commonsware.com/misc/test2.3gp");
			
			InitTask init = new InitTask();
			init.execute(5);
			isFirstStart = false;
		} else {
			waiting.setVisibility(View.INVISIBLE);
			service.onPreviewAvailable();
		}
	}

	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onPostResume()
	 */
	@Override
	protected void onPostResume() {
		// TODO Auto-generated method stub
		super.onPostResume();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		Log.i("SUMPRO", "surfaceDestroyed");
		mcontroller.hide();
		service.getPlayer().setDisplay(null);
		
	}

	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.i("SUMPRO", "onDestroy");
		isFirstStart = false;
		super.onDestroy();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		Log.i("SUMPRO", "onTouchEvent isFirstStart:" + isFirstStart);
		Log.i("SUMPRO", "onTouchEvent service.isPlaying():" + service.isPlaying());
		Log.i("SUMPRO", "onTouchEvent videopath:" + videopath);
		if(service.isPlaying() || service.isPrepared()) {
			mcontroller.show(10000);
		}
		return super.onTouchEvent(event);
	}

	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		service.onPreviewPause();
	}

	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		service.onPreviewResume();
		super.onResume();
	}
	
	public void onPreparedDone() {
		handler.post(new Runnable() {

            public void run() {
                mcontroller.setEnabled(true);
                mcontroller.show();
                waiting.setVisibility(View.INVISIBLE);
            }
		});
		Log.i("SUMPRO", "Video or:" + service.getVideoOrientation());
		Log.i("SUMPRO", "screnn or:" + this.getResources().getConfiguration().orientation);
		if(this.getResources().getConfiguration().orientation != service.getVideoOrientation())
			this.setRequestedOrientation(service.getVideoOrientation());
		mcontroller.show(10000);
		if(!service.isPlaying()) {
			service.start();
		}
	}
	
}
